//
//  TipsViewController.m
//  Cooking
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "TipsViewController.h"
#import "AppMacro.h"
#import "TipsTableViewCell.h"
#import "AsyncGetConnection.h"
#import "TipsFirstPageModel.h"
#import "TipsDetailsViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"

@interface TipsViewController ()<UITableViewDelegate, UITableViewDataSource, MBProgressHUDDelegate>

@property (retain, nonatomic) UITableView    *tips;
@property (retain, nonatomic) NSMutableArray *tableArray;
@property (retain, nonatomic) MBProgressHUD  *HUD;
@property (assign, nonatomic) NSInteger      page;


@end

@implementation TipsViewController

- (void)dealloc {
    [_tips release];
    [_tableArray release];
    [_HUD release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationItem.title = @"小贴士";
    
    [self createTipsTableView];
    [self beginConnect];
    
#pragma mark -
#pragma 下拉刷新
    
    [_tips addHeaderWithCallback:^{
        [AsyncGetConnection asyncGetConnectionWithURL:@"http://121.40.54.242:80/HandheldKitchen/api/found/tblFresh!getTblFreshList.do?is_traditional=0&page=1&pageRecord=10&phonetype=1" parameters:nil block:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *array = [dic objectForKey:@"data"];
            [_tableArray removeAllObjects];
            for (NSDictionary *value in array) {
                TipsFirstPageModel *firstPageModel = [[TipsFirstPageModel alloc] init];
                [firstPageModel setValuesForKeysWithDictionary:value];
                [_tableArray addObject:firstPageModel];
                [firstPageModel release];
            }
            [_tips reloadData];
            [_tips headerEndRefreshing];
        }];
    }];
    
#pragma mark -
#pragma mark 上拉加载
    
    [_tips addFooterWithCallback:^{
        _page = _page + 1;
        [AsyncGetConnection asyncGetConnectionWithURL:[NSString stringWithFormat:@"http://121.40.54.242:80/HandheldKitchen/api/found/tblFresh!getTblFreshList.do?is_traditional=0&page=%ld&pageRecord=10&phonetype=1", _page] parameters:nil block:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *array = [dic objectForKey:@"data"];
            for (NSDictionary *value in array) {
                TipsFirstPageModel *firstPageModel = [[TipsFirstPageModel alloc] init];
                [firstPageModel setValuesForKeysWithDictionary:value];
                [_tableArray addObject:firstPageModel];
                [firstPageModel release];
            }
            [_tips reloadData];
            [_tips footerEndRefreshing];
        }];

    }];
}

#pragma mark -
#pragma mark 网络数据解析
- (void)beginConnect {
    
    [AsyncGetConnection asyncGetConnectionWithURL:@"http://121.40.54.242:80/HandheldKitchen/api/found/tblFresh!getTblFreshList.do?is_traditional=0&page=1&pageRecord=10&phonetype=1" parameters:nil block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"data"];
//        [_tableArray removeAllObjects];
        for (NSDictionary *value in array) {
            TipsFirstPageModel *firstPageModel = [[TipsFirstPageModel alloc] init];
            [firstPageModel setValuesForKeysWithDictionary:value];
            [_tableArray addObject:firstPageModel];
            [firstPageModel release];
        }
        [_tips headerBeginRefreshing];
        [_tips reloadData];
        [_HUD hide:YES];
    }];
}

- (void)createTipsTableView {
    self.tips = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
//    _tips.backgroundColor = [UIColor yellowColor];
    _tips.delegate = self;
    _tips.dataSource = self;
    _tips.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tips];
    [_tips release];
    
    //加载等待
    self.HUD = [[MBProgressHUD alloc] init];
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    _HUD.delegate = self;
    _HUD.backgroundColor = [UIColor clearColor];
    _HUD.detailsLabelText = @"努力加载中...";
    _HUD.dimBackground = YES;
}

#pragma mark - 
#pragma mark tipsTableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    TipsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[TipsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        cell.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    TipsFirstPageModel *model = [_tableArray objectAtIndex:indexPath.row];
    [cell sendValueWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([UIScreen mainScreen].bounds.size.height == 667) {
        return 139;
    } else if ([UIScreen mainScreen].bounds.size.height == 568) {
        return 124;
    } else if ([UIScreen mainScreen].bounds.size.height == 736) {
        return 139;
    }
     return 120;
 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TipsDetailsViewController *tipsDeailsVC = [[TipsDetailsViewController alloc] init];
    tipsDeailsVC.freshId = [[_tableArray objectAtIndex:indexPath.row] valueForKey:@"freshId"];
    [self.navigationController pushViewController:tipsDeailsVC animated:YES];
    [tipsDeailsVC release];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
